<!-- src/components/Category.vue -->
<template>
    <div class="category-container">
        <!-- 左侧侧边栏 -->
        <el-aside width="20%">
            <el-menu :default-active="selectedCategory" @select="selectCategory">
                <el-menu-item v-for="category in categories" :key="category" :index="category">
                    <span>{{ category }}</span>
                </el-menu-item>
            </el-menu>
        </el-aside>
        <!-- 主内容区域 -->
        <el-main>
            <div class="category-content">
                <div v-if="filteredItems.length > 0">
                    <div class="product-grid">
                        <div v-for="item in filteredItems" :key="item.productid" class="product-item"
                            @click="handleProductClick(item.productid)">
                            <!-- 点击跳转到商品详情页 -->
                            <img :src=item.productimg :alt="item.title" class="product-image" />
                            <div class="product-info">
                                <p class="product-name">{{ item.title }}</p>
                                <p class="product-price">¥{{ item.price }}</p>
                            </div>
                        </div>
                    </div>
                </div>
                <div v-else>
                    <h2>暂无商品</h2>
                </div>
            </div>
        </el-main>
    </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { ElAside, ElMenu, ElMenuItem, ElMain } from 'element-plus'
import { categories, selectedCategory, items, fetchItems, filteredItems, selectCategory } from '../stores/categoryStore'
import { useRouter } from 'vue-router'

const router = useRouter()

const handleProductClick = (productId) => {
    console.log('Clicked product ID:', productId) // 打印 productid
    router.push(`/product/${productId}`)

    console.log('路由路径：', router.currentRoute.value.path)
}

onMounted(() => {
    fetchItems()
})
</script>

<style scoped>
/* 调整菜单项样式示例 */
:deep(.el-menu-item) {
    padding: 0 20px !important;
}

.category-container {
    display: flex;
    height: 100vh;
    /* 确保容器有明确高度 */
}

.el-aside {
    flex: 0 0 20%;
    /* 固定宽度 */
    height: 100%;
    /* 填充高度 */
}

.el-menu {
    height: 100%;
    /* 菜单填满侧边栏 */
}

.el-main {
    flex: 1;
    /* 填充剩余空间 */
    padding: 20px;
}

.category-content {
    background: white;
    border-radius: 8px;
    padding: 20px;
}

.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 20px;
}

.product-item {
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    cursor: pointer;
}

.product-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
}

.product-info {
    padding: 10px;
    text-align: center;
}

.product-name {
    font-size: 16px;
    color: #333;
    margin: 0;
}

.product-price {
    color: #ff6b6b;
    font-size: 18px;
    font-weight: bold;
    margin: 5px 0 0;
}
</style>